﻿using AutoTest.Business.Dto.Goods;
using AutoTest.Business.Dto.Product;
using AutoTest.Business.IServices;
using AutoTest.Business.IServices.Common;
using AutoTest.Business.Vo.Goods;
using AutoTest.Business.Vo.Product;
using AutoTest.Common;
using AutoTest.Common.Extension;
using AutoTest.Domain.Entities;
using AutoTest.Domain.Enums;
using AutoTest.Framework.Exceptions;
using AutoTest.Framework.Ioc;
using AutoTest.Framework.Pager;
using EasyTool;
using Mapster;
using SqlSugar;

namespace AutoTest.Business.Services
{
    /// <summary>
    /// 产品服务
    /// </summary>
    public class ProductService(ISqlSugarClient db,IJwtService jwtService) : IProductService, IScoped
    {
        private readonly ISqlSugarClient _db = db;
        private readonly IJwtService _jwtService = jwtService;

        #region 商品管理
        /// <summary>
        /// 添加商品
        /// </summary>
        /// <param name="addDto"></param>
        /// <returns></returns>
        public async Task<ProductDetailVo> AddProductAsync(ProductAddDto addDto)
        {
          
            var productEntity = addDto.Adapt<Product>();
            productEntity.CreateTime = DateTime.Now;
            productEntity.Id = IdUtil.SnowflakeId();
            productEntity.CreateBy = _jwtService.GetCurrentAdminUser()?.Id ?? 0;
            productEntity.CreateTime = DateTime.Now;
            await _db.Insertable(productEntity).ExecuteCommandAsync();

            var vo = productEntity.Adapt<ProductDetailVo>();
            return vo;
        }

        /// <summary>
        /// 编辑商品
        /// </summary>
        /// <param name="editDto"></param>
        /// <returns></returns>
        public async Task<ProductDetailVo> EditProductAsync(ProductEditDto editDto)
        {
            if (editDto == null) throw new BusinessException("未获取到更新信息");

            var productEntity = await _db.Queryable<Product>().FirstAsync(t=>t.Id == editDto.Id) ?? throw new BusinessException($"未能找到商品Id为【{editDto.Id}】的商品信息");
            productEntity.CopyFrom(editDto);
            await _db.Updateable(productEntity).ExecuteCommandAsync();

            var vo = productEntity.Adapt<ProductDetailVo>();
            return vo;
        }
        #endregion

        /// <summary>
        /// 查询列表
        /// </summary>
        /// <param name="searchDto"></param>
        /// <returns></returns>
        public async Task<List<ProductListVo>> SearchProductListAsync(ProductListSearchDto searchDto)
        {
            searchDto ??= new ProductListSearchDto();
            var query = _db.Queryable<Product>()
                    .Where(x => x.Status == EnumStatus.Enable)
                    .WhereIF(searchDto.Keyword.IsNotEmpty(), x => 
                            x.ProductName.Contains(searchDto.Keyword)
                        ||  x.ProductDesc.Contains(searchDto.Keyword)
                        ||  x.BriefDesc.Contains(searchDto.Keyword)
                    );
            var dataList = await query.ToListAsync();
            return dataList.Adapt<List<ProductListVo>>();
        }
    }
}
